<template>
    <div class="edit-dialog-container">
        <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px" destroy-on-close>
            <el-form ref="dialogFormRef" :model="state.content" :rules="formRules" size="default" label-width="90px">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item prop="title" label="字典名称">
                            <el-input v-model="state.content.title" placeholder="请输入字典名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item prop="label" label="字典类型">
                            <el-select v-model="state.content.label" placeholder="输入类型" clearable>
                                <el-option label="单选框" :value="1"></el-option>
                                <el-option label="复选框" :value="2"></el-option>
                                <el-option label="文本框" :value="3"></el-option>
                                <el-option label="下拉列表" :value="4"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item prop="isShow" label="表格显示">
                            <el-switch v-model="state.content.isShow" inline-prompt
                                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #F47273" :active-value="1"
                                :inactive-value="-1" active-text="是" inactive-text="否" />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item prop="sortable" label="排序">
                            <el-input-number v-model="state.content.sortable" placeholder="排序" :step="1" :min="1"
                                clearable></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item prop="required" :label="state.content.label === 3 ? '必须填写' : '必须选择'">
                            <el-switch v-model="state.content.required" inline-prompt
                                style="--el-switch-on-color: #F47273; --el-switch-off-color: #13ce66" :active-value="1"
                                :inactive-value="-1" active-text="是" inactive-text="否" />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item prop="options" label="默认值" v-if="state.content.label === 2">
                            <el-input v-model="state.content.defaultOptions" placeholder="默认值" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item prop="options" v-if="state.content.label !== 3" label="选项">
                            <el-input v-model="state.content.srcData" :rows="8" type="textarea" @input="srcDataChange"
                                clearable placeholder="多个选项之间用  ;  隔开" />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item prop="options" :label="`${state.content.label === 1 ? '点击' :
                            state.content.label === 2 ? '输入' :
                                state.content.label === 3 ? '输入' : '选择'
                            }默认`">
                            <!-- label:1 单选 -->
                            <el-radio-group v-if="state.content.label === 1" v-model="state.content.defaultOption"
                                class="ml-4">
                                <el-radio v-for="item in state.content.options" :label="item" size="large"></el-radio>
                            </el-radio-group>
                            <!-- label:2 多选 -->
                            <el-checkbox-group v-if="state.content.label === 2" v-model="state.content.defaultOptions">
                                <el-checkbox v-for="item in state.content.options" :label="item" border />
                            </el-checkbox-group>
                            <!-- label:3 文本输入 -->
                            <el-input v-if="state.content.label === 3" v-model="state.content.defaultOption"
                                clearable></el-input>
                            <!-- label:4 下拉 -->
                            <el-select v-if="state.content.label === 4" v-model="state.content.defaultOption" clearable>
                                <el-option v-for="item in state.content.options" :label="item" :value="item"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onSubmitContinue" type="primary" size="default" v-if="state.dialog.type === 0">{{
                        t('message.btnName.continueSubmit') }}</el-button>
                    <el-button @click="onSubmit" type="success" size="default">{{ t('message.btnName.submit')
                    }}</el-button>
                    <el-button @click="onCancel" size="default">{{ t('message.btnName.cancel') }}</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang="ts" >
import { reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { ElMessage } from 'element-plus';
import { usePersonDictCreateApi, usePersonDictQueryApi, usePersonDictUpdateApi } from '/@/api/person/dict';

// 定义子组件向父组件传值/事件
const { t } = useI18n();
const emit = defineEmits(['refresh']);

// 定义变量内容
const dialogFormRef = ref();

const useContent = () => {
    return {
        code: '',
        title: '', // 用户姓名
        label: 1,
        isShow: 1,
        required: -1,
        sortable: 1,
        options: [] as String[],
        defaultOption: '',
        defaultOptions: [],
        srcData: '',
    }
}

const state = reactive({
    content: {} as any,
    dialog: {
        isShowDialog: false,
        type: 0,
        title: '',
    },
});

// 表单校验
const formRules = ref({
    code: [
        { required: true, message: '必填项不能为空', trigger: 'blur' },
        { min: 1, max: 40, message: '工号必须1-40位字母数字组合而成', trigger: 'blur' },
        // { pattern: /^[a-z1-9]/, message: '工号必须非0数字或小写字母开头', trigger: 'blur' },
    ],
    sortable: [],
    name: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
    mobile: [{ pattern: /^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$/, message: '请输入正确手机号', trigger: 'blur' },],
})



// 打开弹窗
const openDialog = async (code?: string) => {
    state.dialog.isShowDialog = true
    if (code) {
        state.dialog.title = t('message.btnName.edit')
        state.dialog.type = 1
        queryItem(code)
    } else {
        state.dialog.title = t('message.btnName.addRow')
        state.dialog.type = 0
        state.content = useContent()
    }
};
// 关闭弹窗
const closeDialog = () => {
    state.dialog.isShowDialog = false;
    state.content = useContent()
    emit('refresh');
};
// 取消
const onCancel = () => {
    closeDialog();
};
const queryItem = (code: string) => {
    usePersonDictQueryApi(code).then(({ content }) => {
        state.content = content
    }).then(() => {
        state.content.srcData = state.content.options.join(';')
    })
}
// 提交
const onSubmit = () => {
    if (state.dialog.type === 1) {
        usePersonDictUpdateApi(state.content).then(({ content }) => {
            console.log(content);
        }).then(() => {
            closeDialog()
        })
    } else {
        usePersonDictCreateApi(state.content).then(({ content }) => {
            console.log(content);
        }).then(() => {
            closeDialog()
        })
    }
};
// 提交
const onSubmitContinue = () => {
    usePersonDictCreateApi(state.content).then(() => {
        ElMessage({
            type: 'success',
            message: '新增成功'
        })
    }).then(() => {
        state.content = useContent()
    })
};
const srcDataChange = () => {
    state.content.srcData = state.content.srcData.replace("；", ";")
    state.content.options = state.content.srcData.split(";")
}
defineExpose({
    openDialog,
});
</script>


<style scoped lang="scss">
:deep(.el-dialog__header) {
    margin: 0;
    border-bottom: 1px dashed rgba(0, 0, 0, .2);
}

:deep(.el-dialog__footer) {
    border-top: 1px dashed rgba(0, 0, 0, .2);
}
</style>